<?php

/**
 * Created by PhpStorm.
 * User: Evan
 * Date: 15/12/28
 * Time: 下午5:33
 */
class Publics extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $name   = trim($this->input->get_post('name'));
        $cid    = $this->input->get_post('cid');
        $citys  = (int)$this->input->get_post('city');
        $time   = $this->input->get_post('create_time');
        $status = $this->input->get_post('status');


        $where = array();
        $name  && $where['name'] = $name;
        ($cid || $cid == '0')   && $where['cid'] = (int)$cid;
        $citys && $where['city'] = $citys;
        ($status || $status === '0') && $where['status'] = $status;


        if ($time)
        {
            $eTime = explode('-', $time);
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_public', 'public');
        $this->load->model('Model_public_star', 'ps');
        $d = (array)$this->ps->countStar(1);


        $totalNum   = $this->public->count($where);
        $publicInfo = $this->public->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Publics/index', $totalNum, $limit, '&'.http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $publicData = $this->public->lists(0, 1000, array(), 'name');


        $this->load->model('Model_public_cate', 'pc');
        $cateData = $this->pc->lists(0, 10000);


        $this->load->model('model_area', 'area');
        $province = $this->area->province_list(0, 10000);
        $city = $this->area->city_list(0, 10000);
        $area = $this->area->area_list(0, 10000);


        $data = array(
            'title'       => '公众号列表',
            'public_info' => $publicInfo,
            'public_data' => $publicData,
            'cate_data'   => sortData($cateData),
            'pageHtml'    => $pageHtml,
            'time'        => $time,
            'cid'         => $cid,
            'status'      => $status,
            'name'        => $name,
            'citys'       => $citys,
            'province'    => $province,
            'city'        => $city,
            'area'        => $area,
            'totalNum'    => $totalNum,
        );

        $this->load->view('agent/public/index', $data);
    }


    public function get_special()
    {
        $cid = (int)$this->input->get_post('cid');

        $where = array('status' => SPECIAL_STATUS_NORMAL);
        $cid && $where['cid'] = $cid;

        $this->load->model('Model_public_special', 'ps');
        $list = $this->ps->lists(0, 100, $where);


        $data = array();
        foreach ($list as $v)
            $data[] = array('value' => $v['sid'], 'text' => $v['name']);


        echo json_encode($data);
    }


    public function create()
    {
        $pid = (int)$this->uri->segment(3);


        $this->load->model('Model_public_cate', 'pc');
        $cateData = $this->pc->lists(0, 10000);


        $this->load->model('model_area', 'area');
        $province = $this->area->province_list(0, 10000);


        $data = array(
            'title'     => '编辑公众号',
            'province'  => $province,
            'cate_data' => sortData($cateData),
        );


        if ($pid)
        {
            $this->load->model('Model_public', 'public');
            $data['data'] = $this->public->getById($pid);

            $data['city'] = $this->area->city_list(0, 1000, array('parent_id' => $data['data']['province']));
            $data['area'] = $this->area->area_list(0, 1000, array('parent_id' => $data['data']['city']));
        }


        $this->load->view('agent/public/create', $data);
    }


    public function save()
    {
        $pid = (int)$this->input->get_post('pid');

        $data = array(
            'uid'       => $this->uInfo['staff_id'],
            'uname'     => $this->uInfo['login_name'],
            'name'      => trim($this->input->get_post('name')),
            'cid'       => (int)($this->input->get_post('cid')),
            'account'   => trim($this->input->get_post('account')),
            'auth_company' => trim($this->input->get_post('auth_company')),
            'depth'     => trim($this->input->get_post('depth')),
            'introduce' => trim($this->input->get_post('introduce')),
            'header'    => (int)($this->input->get_post('header')),
            'qr'        => (int)($this->input->get_post('qr')),
            'sort'      => (int)($this->input->get_post('sort')),
            'title'     => trim($this->input->get_post('title')),
            'keyword'   => trim($this->input->get_post('keyword')),
            'descr'     => trim($this->input->get_post('descr')),
            'contact'   => trim($this->input->get_post('contact')),
            'mobile'    => trim($this->input->get_post('mobile')),
            'qq'        => trim($this->input->get_post('qq')),
            'email'     => trim($this->input->get_post('email')),
            'website'   => trim($this->input->get_post('website')),
            'province'  => (int)($this->input->get_post('province')),
            'city'      => (int)($this->input->get_post('city')),
            'area'      => (int)($this->input->get_post('area')),
            'status'    => (int)($this->input->get_post('status')),
        );


        if (empty($data['name']) || !$data['cid'] || empty($data['account']) || !$data['header'] || !$data['qr'])
            show_error('参数不全!');



        $this->load->model('Model_public_cate', 'pc');
        $item = $this->pc->getById($data['cid']);
        if (empty($item))
            show_error('分类不存在!');


        $this->load->model('Model_public', 'public');
        $rs = $this->public->save($data, $pid ? $pid : null);
        if (!$rs)
            show_error('保存失败!');


        $this->load->helper('url');
        redirect(url('admin').'Publics/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $pid = (int)$this->input->get_post('pid');

            if (!$pid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public', 'public');
            $item = $this->public->getById($pid);

            if (empty ($item))
            {
                $response = error(300003);
                break;
            }


            $res = $this->public->delete(array('pid' => $pid));
            if (!$res)
            {
                $response = error(300004);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    public function audit()
    {
        $response = array('code' => '0', 'msg' => '审核成功');

        do {
            $pids = $this->input->get_post('pids');
            $status = (int)$this->input->get_post('status');


            if (empty($pids))
            {
                $response = error(100000);
                break;
            }


            if (!array_key_exists($status, config_item('public_status')))
            {
                $response = error(300005);
                break;
            }


            $pids = (false === strpos($pids, ',')) ? array($pids) : explode(',', $pids);
            $pids = array_filter($pids);


            $ids = array();
            $this->load->model('Model_public', 'public');
            $data = $this->public->getByIn('pid', $pids);
            foreach ($data as $v)
                $ids[] = $v['pid'];


            $rs = $this->public->updateByIn('pid', $ids, array('status' => $status));
            if (!$rs)
            {
                $response = error(300006);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    public function change_cate()
    {
        $response = array('code' => '0', 'msg' => '变更成功');

        do {
            $pid = (int)$this->input->get_post('pid');
            $cid = (int)$this->input->get_post('cid');


            if (!$pid || !$cid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public', 'public');
            $public_item = $this->public->getById($pid);
            if (empty($public_item))
            {
                $response = error(300003);
                break;
            }


            if ($cid == $public_item['cid'])
            {
                $response = error(300009);
                break;
            }


            $this->load->model('Model_public_cate', 'pc');
            $cate_item = $this->pc->getById($cid);
            if (empty($cate_item))
            {
                $response = error(300000);
                break;
            }

            $rs = $this->public->save(array('cid' => $cid), $pid);
            if (!$rs)
            {
                $response = error(300010);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}